import objects
from utils import RenderRepr, default_hidden_version


class NormalWall(objects.Wall):
    render_repr = RenderRepr("#", (135, 135, 135), (165, 165, 165))
    render_repr.hidden_version = default_hidden_version(render_repr)
    is_solid = True


class NormalFloor(objects.Floor):
    render_repr = RenderRepr(".", (110, 110, 110), (90, 90, 90))
    render_repr.hidden_version = default_hidden_version(render_repr)

    def __init__(self):
        super().__init__()
        self.is_solid = True


class Teleporter(objects.Floor):
    render_repr = RenderRepr("O", (110, 110, 110), (90, 90, 90))
    render_repr.hidden_version = default_hidden_version(render_repr)
    is_solid = True

    def on_entered(self, who):
        if isinstance(who, NormalWall):
            return
        if who is self:
            return
        tile = self.tile.level.random_tile()
        while not who.can_enter(tile):
            tile = self.tile.level.random_tile()
        who.move_to_tile(tile)


class Boulder(objects.Wall):
    render_repr = RenderRepr("o", (200, 100, 0), None)
    render_repr.hidden_version = default_hidden_version(render_repr)
    is_solid = True

    def push(self, direction):
        target_tile = self.tile.around_tiles[direction]
        if target_tile and self.can_enter(target_tile):
            self.move_to_tile(target_tile)
            return True
        return False


class RollingBoulder(Boulder):
    render_repr = RenderRepr("o", (150, 75, 0), None)
    render_repr.hidden_version = default_hidden_version(render_repr)
    rolling_speed = 50

    def __init__(self):
        super().__init__()
        self.rolling_direction = None

    def push(self, direction):
        self.rolling_direction = direction
        self.game.add_task(self.rolling_speed, self.roll)

    def roll(self):
        if not self.tile:
            return
        target_tile = self.tile.around_tiles[self.rolling_direction]
        if target_tile and self.can_enter(target_tile):
            self.move_to_tile(target_tile)
            self.game.add_task(self.rolling_speed, self.roll)
        else:
            self.rolling_direction = None


class BouncyBoulder(RollingBoulder):
    render_repr = RenderRepr("o", (110, 50, 0), None)
    render_repr.hidden_version = default_hidden_version(render_repr)

    def roll(self):
        if not self.tile:
            return
        target_tile = self.tile.around_tiles[self.rolling_direction]
        if target_tile and self.can_enter(target_tile):
            self.move_to_tile(target_tile)
            self.game.add_task(self.rolling_speed, self.roll)
        else:
            self.rolling_direction = (self.rolling_direction + 4) % 8
            self.game.add_task(self.rolling_speed, self.roll)


class Rubble(objects.Stuff):
    def __init__(self, obj):
        super().__init__()
        self.render_repr = RenderRepr(",", obj.render_repr.fg, None)


class BootsOfSpeed(objects.Item):
    render_repr = RenderRepr("]", (255, 255, 0), None)
    render_repr.hidden_version = default_hidden_version(render_repr)

    def __init__(self):
        super().__init__()
        self.effects["walk_speed"] = ("*", 0.8)
